<?php

namespace app\admin\model\order;

use app\admin\logic\KuaiDi;
use app\common\model\goods\GoodsModel;
use app\common\model\order\OrderAction;
use app\common\model\order\OrderGoodsModel;
use think\Model;
use traits\model\SoftDelete;

class Order extends Model
{

    use SoftDelete;

    

    // 表名
    protected $table = 'order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'order_settlement_type_text',
        'is_settlement_text',
//        'settlement_pay_type_text',
        'is_pay_text',
        'pay_type_text',
        'pay_time_text',
        'is_close_text',
        'close_time_text',
        'status_text',
        'cancel_time_text',
        'delivery_time_text',
        'received_time_text',
        'is_sales_text',
        'platform_text',
        'dispatch_type_text',
        'is_burst_order_text',
    ];


    public function setPay($order, $admin_id)
    {
        //设为已支付
        //修改订单状态
        $order->status = 1;
        $order->is_pay = 1;
        $order->pay_time = time();
        $order->pay_price = $order->total_fee;
        $order->save();

        // 库存扣减方式:place=拍下减库存,pay=付款减库存
        // 扣除库存(付款减库存)
        $order_goods = OrderGoodsModel::field('goods_id,goods_spec_id,num')
            ->with(['goods' => function($query){
                $query->where('stock_type','pay');
            }])
            ->where(['order_id' => $order->id])
            ->select();
        if($order_goods){
            foreach ($order_goods as $row){
                GoodsModel::updateStock($row->goods_id,$row->goods_spec_id,$row->num);
            }
        }
        $oper = [
            'id' => $admin_id
        ];
        OrderAction::operAdd($order, null, $oper, 'admin', '订单设为已支付');
    }


    public function setStatus($order, $admin_id, $status)
    {
        // 取消/设为已完成
        switch ($status){
            // 已完成
            case 3:
                $order->status = 3;
                $order->is_close = 1;
                $order->close_time = time();
                $order->save();
                $oper = [
                    'id' => $admin_id
                ];
                OrderAction::operAdd($order, null, $oper, 'admin', '订单设为已完成');
                break;
            // 取消
            case 5:
                $order->status = 5;
                $order->is_close = 1;
                $order->cancel_time = time();
                $order->close_time = time();
                $order->save();
                // 库存回滚
                $orderGoods = OrderGoodsModel::where(['id' => $order->id])->select();
                foreach ($orderGoods as $row){
                    $goods = GoodsModel::get($row->goods_id);
                    if($goods->stock_type == 'place'){
                        GoodsModel::stockRollback($row->goods_id,$row->goods_spec_id,$row->num);
                    }
                }
                $oper = [
                    'id' => $admin_id
                ];
                // 退回优惠券
                if($order->coupon_id > 0){
                    \app\common\model\UserCouponModel::where(['id' => $order->coupon_id])->update(['status' => 1, 'usetime' => '']);
                    OrderAction::operAdd($order, null, $oper, 'admin', '订单取消恢复优惠券');
                }
                OrderAction::operAdd($order, null, $oper, 'admin', '订单设为已取消');
                break;
        }
    }




    public function getIsBurstOrderList()
    {
        return ['0' => __('Is_burst_order 0'), '1' => __('Is_burst_order 1')];
    }

    public function getExpressList()
    {
//        $array = json_decode('[{"name":"顺丰速运","code":"shunfeng"},{"name":"京东物流","code":"jd"}]',true);
//        return $array;
        return KuaiDi::getExpress();
    }

    public function getDispatchTypeList()
    {
        return ['express' => __('Dispatch_type express'), 'ziti' => __('Dispatch_type ziti')];
    }

    public function getOrderSettlementTypeList()
    {
        return ['cash' => __('Order_settlement_type cash'), 'credit' => __('Order_settlement_type credit')];
    }

    public function getIsSettlementList()
    {
        return ['0' => __('Is_settlement 0'), '1' => __('Is_settlement 1')];
    }

    public function getIsPayList()
    {
        return ['0' => __('Is_pay 0'), '1' => __('Is_pay 1')];
    }

    public function getPayTypeList()
    {
        return ['alipay' => __('Pay_type alipay'), 'wxpay' => __('Pay_type wxpay'), 'wallet' => __('Pay_type wallet'), 'public' => __('Pay_type public'), 'private' => __('Pay_type private')];
    }

//    public function getSettlementPayTypeList()
//    {
//        return ['alipay' => __('Settlement_pay_type alipay'), 'wxpay' => __('Settlement_pay_type wxpay'), 'public' => __('Settlement_pay_type public'), 'private' => __('Settlement_pay_type private')];
//    }

    public function getIsCloseList()
    {
        return ['0' => __('Is_close 0'), '1' => __('Is_close 1')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3'), '4' => __('Status 4'), '5' => __('Status 5')];
    }

    public function getIsSalesList()
    {
        return ['0' => __('Is_sales 0'), '1' => __('Is_sales 1'), '2' => __('Is_sales 2')];
    }

    public function getPlatformList()
    {
        return ['mini' => __('Platform mini'), 'pc' => __('Platform pc'), 'app' => __('Platform app')];
    }


    public function getIsBurstOrderTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_burst_order']) ? $data['is_burst_order'] : '');
        $list = $this->getDispatchTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getDispatchTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['dispatch_type']) ? $data['dispatch_type'] : '');
        $list = $this->getDispatchTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getOrderSettlementTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['order_settlement_type']) ? $data['order_settlement_type'] : '');
        $list = $this->getOrderSettlementTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsSettlementTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_settlement']) ? $data['is_settlement'] : '');
        $list = $this->getIsSettlementList();
        return isset($list[$value]) ? $list[$value] : '';
    }


//    public function getSettlementPayTypeTextAttr($value, $data)
//    {
//        $value = $value ? $value : (isset($data['settlement_pay_type']) ? $data['settlement_pay_type'] : '');
//        $list = $this->getPayTypeList();
//        return isset($list[$value]) ? $list[$value] : '';
//    }


    public function getIsPayTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_pay']) ? $data['is_pay'] : '');
        $list = $this->getIsPayList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPayTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_type']) ? $data['pay_type'] : '');
        $list = $this->getPayTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPayTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_time']) ? $data['pay_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getIsCloseTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_close']) ? $data['is_close'] : '');
        $list = $this->getIsCloseList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCloseTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['close_time']) ? $data['close_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getReceivedTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['received_time']) ? $data['received_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getDeliveryTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['delivery_time']) ? $data['delivery_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getCancelTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['cancel_time']) ? $data['cancel_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getIsSalesTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_sales']) ? $data['is_sales'] : '');
        $list = $this->getIsSalesList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPlatformTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['platform']) ? $data['platform'] : '');
        $list = $this->getPlatformList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setPayTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCloseTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setReceivedTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setDeliveryTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setCancelTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id', [], 'LEFT');
    }
    public function item()
    {
        return $this->hasMany('app\admin\model\order\OrderGoods', 'order_id', 'id');
    }
    public function express()
    {
        return $this->hasMany('app\common\model\order\OrderExpress', 'order_id', 'id');
    }
    public function action()
    {
        return $this->hasMany('app\common\model\order\OrderAction', 'order_id', 'id');
    }
    public function shop()
    {
        return $this->belongsTo('app\admin\model\User', 'shop_id', 'id');
        return $this->belongsTo('app\common\model\ShopModel', 'shop_id', 'id');
    }
    public function claimshop()
    {
        return $this->belongsTo('app\admin\model\User', 'shop_id', 'id');
        return $this->belongsTo('app\common\model\ShopModel', 'shop_id', 'id');
    }
}
